package model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import vo.VendorVO;

import com.ibatis.sqlmap.client.SqlMapClient;

public class VendorDAO {
	private SqlMapClient client;
	private static VendorDAO dao = new VendorDAO();
	private VendorDAO(){
		SqlMapFactory factory = SqlMapFactory.getInstance();
		client = factory.getClient(); 
	}
	public static VendorDAO getInstance(){
		if(dao == null) dao = new VendorDAO();
		return dao;
	}
	public void insert(VendorVO vvo) throws SQLException{
		try{
			client.insert("vendor.insert", vvo);
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("VendorDAO.insert() Exception error");
		}

	}
	public ArrayList<VendorVO> selectAllVendor(String id, int pageNo){
		ArrayList<VendorVO> vlist = null;
		try {
				HashMap map = new HashMap();
				map.put("id", id);
				map.put("pageNo", pageNo);
				vlist = (ArrayList<VendorVO>) client.queryForList("vendor.selectAllVendor",map);
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("VendorDAO.selectAllVendor() Exception error");
		}
		return vlist;
	}
	public VendorVO searchByVendorId(String vendorId, String logId)throws SQLException{
		VendorVO vvo = null;
		HashMap map = new HashMap();
		map.put("id", logId);
		map.put("vendorId", vendorId);	
		try{
			vvo = (VendorVO)client.queryForObject("vendor.searchByVendorId",  map);
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("VendorDAO.searchByVendorId() Exception error");
		}
		return vvo;
	}
	public int selectTotalVendor(String id, String vendorName){
		int totalCnt = 0;
		HashMap map = new HashMap();
		map.put("id", id);
		map.put("vendorName", vendorName);
		try {
			totalCnt = (Integer)client.queryForObject("vendor.select_count",map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return totalCnt;
		
	}

	public VendorVO searchByRegNo(String regNo, String logId)throws SQLException{
		VendorVO vvo = null;
		HashMap map = new HashMap();
		map.put("id", logId);
		map.put("regNo",regNo);
		try{
			vvo = (VendorVO)client.queryForObject("vendor.searchByregNo",  map);
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("VendorDAO.searchByregNo() Exception error");
		}
		return vvo;
	}	
	public ArrayList<VendorVO> searchByVendorName(String vendorName, String id, int pageNo)throws SQLException{
		ArrayList<VendorVO> vlist = new ArrayList<VendorVO>();
		HashMap map = new HashMap();
		map.put("id", id);
		map.put("vendorName", vendorName);	
		map.put("pageNo", pageNo);
		try{
			vlist= (ArrayList<VendorVO>) client.queryForList("vendor.searchByVendorName", map);
		}catch(SQLException e){
			System.out.println("VendorDAO.searchByVendorName() Exception error");
			e.printStackTrace();
		}
		return vlist;
	}
	public void deleteVendor(String id, VendorVO vvo)throws SQLException{
		HashMap map = new HashMap();
		map.put("id", id);
		map.put("vendorId", vvo.getVendorId());	
		client.update("vendor.deleteVendor", map);
	}
	
	public void modifyVendor(VendorVO vvo)throws SQLException{
		client.update("vendor.modifyVendor",vvo);
	}
	
}
